package com.hua.common.commonview;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.hua.common.commontools.DipUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hxl on 2016/3/1.
 */
public class MyChat extends LinearLayout {

    private int mWidth = 0;
    private int mHeight = 0;
    /**
     * 黑色坐标线的画笔
     */
    private Paint blackLinePaint = null;
    /**
     * 画笔的粗细
     */
    private int blackLinePaintWidth = 0;
    /**
     * 刻度的画笔
     */
    private Paint markPaint = null;
    /**
     * 刻度字体大小
     */
    private float markTextSize = 0f;
    /**
     * 其他文本字体大小
     */
    private float textSize = 0f;

    /**
     * 文本的画笔
     */
    private Paint textPaint = null;
    /**
     * 绘制长条的画笔
     */
    private Paint rectanglePaint = null;

    private int rectanglePaintWidth = 30;

    private int top = 50, left = 150, bottom = 100, right = 100;
    private int itemCount = 20;   //单位高度所代表的大小

    private List<ItemBean> list = null;
    private int itemheight = 0;

    private float value0 = 0;
    private float value1 = 0;
    private float value2 = 0;

    private int STATE = 0;

    public void setList(List<ItemBean> list) {
        this.list = list;
    }

    public MyChat(Context context) {
        super(context);
        this.init();
    }

    public MyChat(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public MyChat(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init();
    }

//    public MyChat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//        this.init();
//    }

    private void init() {
        this.initMargin();
        this.initPaint();
        this.initData();
    }

    private void initData() {
        this.list = new ArrayList<ItemBean>();
        for (int i = 0; i < 8; i++) {
            ItemBean bean = new ItemBean();
            bean.setTopText(10 * i + "次");
            bean.setCount(10 * i);
            bean.setBottomText(i + "日");
            list.add(bean);
        }
    }

    private void initMargin() {
        this.top = DipUtil.Dp2px(getContext(), 25);
        this.left = DipUtil.Dp2px(getContext(), 35);
        this.bottom = DipUtil.Dp2px(getContext(), 35);
        this.right = DipUtil.Dp2px(getContext(), 25);
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        this.blackLinePaintWidth = DipUtil.Dp2px(getContext(), 1);
        blackLinePaint = new Paint();
        blackLinePaint.setAntiAlias(true);
        blackLinePaint.setStyle(Paint.Style.STROKE);
        blackLinePaint.setStrokeWidth(this.blackLinePaintWidth);
        blackLinePaint.setAntiAlias(true); // 消除锯齿
        blackLinePaint.setDither(true);
        blackLinePaint.setStrokeCap(Paint.Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        blackLinePaint.setColor(Color.parseColor("#ffcccccc"));


        markTextSize = DipUtil.Sp2px(getContext(), 12f);
        markPaint = new Paint();
        markPaint.setTextSize(markTextSize);
        markPaint.setAntiAlias(true);
        markPaint.setStyle(Paint.Style.FILL);
        markPaint.setStrokeWidth(this.blackLinePaintWidth);
        markPaint.setAntiAlias(true); // 消除锯齿
        markPaint.setDither(true);
        markPaint.setStrokeCap(Paint.Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        markPaint.setColor(Color.parseColor("#ffff0000"));

        textSize = DipUtil.Sp2px(getContext(), 13f);
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setStrokeWidth(this.blackLinePaintWidth);
        textPaint.setAntiAlias(true); // 消除锯齿
        textPaint.setDither(true);
        textPaint.setTextSize(textSize);
        textPaint.setStrokeCap(Paint.Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        textPaint.setColor(Color.parseColor("#ff333333"));


        rectanglePaintWidth = DipUtil.Dp2px(getContext(), 1);
        rectanglePaint = new Paint();
        rectanglePaint.setAntiAlias(true);
        rectanglePaint.setStyle(Paint.Style.FILL);
        rectanglePaint.setStrokeWidth(this.rectanglePaintWidth);
        rectanglePaint.setAntiAlias(true); // 消除锯齿
        rectanglePaint.setDither(true);
        rectanglePaint.setStrokeCap(Paint.Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        rectanglePaint.setColor(Color.parseColor("#0085cf"));
    }

    /**
     * 创建一个动画
     */
    public void startValueAnim() {

       createValueAnim(new onValueAnimUpdate() {
           @Override
           public void onupdate(float value) {
               value0=value;
               invalidate();
           }
           @Override
           public void onAnimEnd() {
               STATE=1;
                 createValueAnim(new onValueAnimUpdate() {
                     @Override
                     public void onupdate(float value) {
                         value1=value;
                         invalidate();
                     }
                     @Override
                     public void onAnimEnd() {
                         STATE=2;
                         createValueAnim(new onValueAnimUpdate() {
                             @Override
                             public void onupdate(float value) {
                                 value2=value;
                                 invalidate();
                             }
                             @Override
                             public void onAnimEnd() {
                             }
                         },1500).start();
                     }
                 },1500).start();
           }
       },1000).start();
    }

    private ValueAnimator createValueAnim(final onValueAnimUpdate onValueAnimUpdate, int duration) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                onValueAnimUpdate.onAnimEnd();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                onValueAnimUpdate.onupdate((Float) animation.getAnimatedValue());
            }
        });
        valueAnimator.setDuration(duration);
        return valueAnimator;
    }

    interface onValueAnimUpdate {
        void onupdate(float value);

        void onAnimEnd();
    }

    /**
     * 获取控件自身的宽度
     */
    private void initWH() {
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        rectanglePaintWidth = mWidth / 10;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.initWH();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (STATE) {
            case 0:
                drawXY(canvas);
                break;
            case 1:
                drawXY(canvas);
                drawHl(canvas);
                break;
            case 2:
                drawXY(canvas);
                drawHl(canvas);
                drawRectangle(canvas);
                break;
        }



    }

    /**
     * 绘制坐标系
     *
     * @param canvas
     */
    private void drawXY(Canvas canvas) {
//        , left, mHeight - bottom, mWidth - right, mHeight - bottom
        float[] pts = {left, top, left, (mHeight - bottom)*value0};        //数据
        canvas.drawLines(pts, blackLinePaint);                   //绘制多条直线
    }

    /**
     * 绘制横线和刻度
     */
    private void drawHl(Canvas canvas) {
        itemheight = (mHeight - 2 * top - bottom) / 5;
        for (int i = 0; i < 6; i++) {
            if ((mWidth - right) * value1 > left) {
                canvas.drawLine(left, mHeight - bottom - itemheight * i, (mWidth - right) * value1, mHeight - bottom - itemheight * i, blackLinePaint);
            }
            if (value1 == 1) {
                canvas.drawText("" + itemCount * i, left - 60, mHeight - bottom - itemheight * i, markPaint);
            }
        }

    }

    /**
     * 绘制柱状图
     *
     * @param canvas
     */
    private void drawRectangle(Canvas canvas) {
        if (list != null && list.size() > 0) {
            int RectangleWidth = DipUtil.Dp2px(getContext(), 13);
            int itemWidth = (mWidth - left - right) / list.size();
            for (int i = 0; i < list.size(); i++) {
                ItemBean itemBean = list.get(i);
                int startX = (left + itemWidth / 2 - RectangleWidth / 2 + itemWidth * i);
                int height = (int) ((itemBean.getCount() / itemCount) * itemheight);
                int startY = mHeight - bottom - height;
                int endX = startX + RectangleWidth;
                int endY = startY + height;
                Rect rect = new Rect(startX, startY, endX, (int) (endY * value2));
                canvas.drawRect(rect, rectanglePaint);                      //绘制矩形
                if (value2 == 1) {
                    canvas.drawText(itemBean.getTopText(), (startX + endX) / 2 - textSize, startY - 2, textPaint);
                    canvas.drawText(itemBean.getBottomText(), (startX + endX) / 2 - textSize, endY + textSize, textPaint);
                }
            }
        }
    }


    public static class ItemBean {
        public ItemBean(){}
        private String topText = null;
        private float count = 0f;
        private String bottomText = null;

        public String getBottomText() {
            return bottomText;
        }

        public void setBottomText(String bottomText) {
            this.bottomText = bottomText;
        }

        public String getTopText() {
            return topText;
        }

        public void setTopText(String topText) {
            this.topText = topText;
        }

        public float getCount() {
            return count;
        }

        public void setCount(float count) {
            this.count = count;
        }

    }
}
